﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Data.SqlClient;
using COMOSOFT.Class;
using MSoft.Shipping.Control;
using DevExpress.XtraGrid.Views.Grid;
using DevExpress.XtraEditors;

namespace COMOSOFT.MatarialManagement.Forms
{
    public partial class FrmBAGaneralView : Form
    {
        ClassShare cls = new ClassShare("FrmBAGaneralView");
        Task tak;       
        FrmBookMA frmBMA = null;
        FrmBAPreview frmBAPreview = null;
        object items;
        string Items = null;     
        int NQty, EQty;
        double Unitprice;        
        DataTable dtStQty;

        public FrmBAGaneralView(object _Items)
        {
            InitializeComponent();
            this.items = _Items;
        }
        public void LoadData()
        {
            try
            {
                if (tak == null || tak.Status != TaskStatus.Running)
                {
                    splashScreenManager1.ShowWaitForm();                    
                    gridControl_BAGeneralView.DataSource = null;
                    tak = Task.Factory.StartNew(() => sp_BAGaneralViewTableAdapter.Fill(dataSetBalancing.sp_BAGaneralView,items));
                    tak.ContinueWith(tt => FillTable());
                }
                bandedGridView_Balance.ColumnPanelRowHeight = 30;
                new BandedCellSelectionHelper(gridControl_BAGeneralView);               
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }            
        }
        private delegate void dlgFillTable();
        private void FillTable()
        {
            try
            {
                if (InvokeRequired)
                {
                    this.Invoke(new dlgFillTable(FillTable));
                }
                else
                {
                    gridControl_BAGeneralView.DataSource = dataSetBalancing.sp_BAGaneralView;
                    splashScreenManager1.CloseWaitForm();
                }
            }
            catch
            {
                MessageBox.Show("Progess fail");
            }
        }

        #region[LoadDetails]
        private void bandedGridView_Balance_FocusedRowChanged(object sender, DevExpress.XtraGrid.Views.Base.FocusedRowChangedEventArgs e)
        {
            try
            {
                GridView view = sender as GridView;
                Items = null;
                if (e.FocusedRowHandle >= 0)
                {
                    if (!view.IsGroupRow(view.FocusedRowHandle))
                    {
                        Items = view.GetDataRow(e.FocusedRowHandle)["MALDIDs"].ToString();
                    }
                    sp_BanlanceDetailsTableAdapter.Fill(dataSetBalancing.sp_BanlanceDetails, Items, Convert.ToInt32(view.GetFocusedDataRow()["MADID"]));
                    dockPanel_BalanceDetails.Visibility = DevExpress.XtraBars.Docking.DockVisibility.Visible;
                    sp_BAUseStockTableAdapter.Fill(dataSet_WareHouse.sp_BAUseStock, Convert.ToInt32(bandedGridView_Balance.GetFocusedDataRow()["MADID"]));
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
        #endregion

        #region [Preview]
        public void BAPreview()
        {
            frmBAPreview = new FrmBAPreview(bandedGridView_Balance);
            frmBAPreview.LoadData();
            frmBAPreview.Show();
        }
        #endregion

        #region[Book]
        private void bookToolStripMenuItem_Click(object sender, EventArgs e)
        {
            try
            {
                if (gridView_BalanceDetails.Columns["NeedQty"].SummaryItem.SummaryValue.ToString() != "")
                {                   
                    NQty = Convert.ToInt32(gridView_BalanceDetails.Columns["NeedQty"].SummaryItem.SummaryValue);
                    if (bandedGridView_Balance.GetFocusedDataRow()["StockQty"].ToString() != "")
                        NQty -= Convert.ToInt32(bandedGridView_Balance.GetFocusedDataRow()["StockQty"]);
                }
                else
                    NQty = 0;
                if (gridView_BalanceDetails.Columns["ExptQty"].SummaryItem.SummaryValue.ToString() != "")
                    EQty = Convert.ToInt32(gridView_BalanceDetails.Columns["ExptQty"].SummaryItem.SummaryValue);
                else
                    EQty = 0;
                if (!Convert.IsDBNull(bandedGridView_Balance.GetFocusedDataRow()["UnitPrice"]))
                    Unitprice = Convert.ToInt32(gridView_BalanceDetails.Columns["UnitPrice"]);
                else
                    Unitprice = 0;
                int MAID = Convert.ToInt32(bandedGridView_Balance.GetFocusedDataRow()["MAID"]);
                int MADID = Convert.ToInt32(bandedGridView_Balance.GetFocusedDataRow()["MADID"]);
                string MALDIDs = bandedGridView_Balance.GetFocusedDataRow()["MALDIDs"].ToString();
                frmBMA = new FrmBookMA(-1) { Text = "Add New Book Material" };
                frmBMA.Show();
                frmBMA.AddNew();
                frmBMA.loadData();
                frmBMA.ShowControl();
                frmBMA.SetValue(MAID, MADID, Unitprice, NQty - EQty, MALDIDs);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
        #endregion

        #region[Refresh]
        private void refreshToolStripMenuItem_Click(object sender, EventArgs e)
        {
            LoadData();
        }
        #endregion

        #region[Preview]
        private void previewToolStripMenuItem_Click(object sender, EventArgs e)
        {
            BAPreview();
        }
        #endregion

        #region[Save]
        public void SaveBA()
        {
            dockPanel_SaveBA.Show();           
        }
        private void bnt_BASave_Click(object sender, EventArgs e)
        {
            try
            {
                cls.UpdateData("sp_BASave", new string[] { "@MALIDs", "@Name", "@CreateDate", "@Creater" }, new object[] { items, txt_Name.Text, DateTime.Now, ClassShare.Usid });
                dockPanel_SaveBA.Close();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void bnt_BACancel_Click(object sender, EventArgs e)
        {
            dockPanel_SaveBA.Close();
        }
        #endregion     

        private void bnt_Cancel_Click(object sender, EventArgs e)
        {
            dockPanel_UseStock.Close();
        }

        private void bnt_OK_Click(object sender, EventArgs e)
        {
            try
            {               
                GridView view = gridView_UseStock as GridView;
                if (view.FocusedRowHandle >= 0)
                {
                    int MAID = Convert.ToInt32(bandedGridView_Balance.GetFocusedDataRow()["MAID"]);
                    int MADID = Convert.ToInt32(bandedGridView_Balance.GetFocusedDataRow()["MADID"]);
                    string MALDID = bandedGridView_Balance.GetFocusedDataRow()["MALDIDs"].ToString();
                    int MAEXCID = Convert.ToInt32(view.GetFocusedDataRow()["MAEXCID"]);
                    double Qty = Convert.ToInt32(view.GetFocusedDataRow()["ExcQty"]);
                    cls.UpdateData("sp_WHExctAllocateInser", new string[] { "@MAEXCID", "@MALDID", "@MAID", "@MADID", "@Qty" }, new object[] { MAEXCID, MALDID, MAID, MADID, Qty });
                    LoadData();                    
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
        private void bandedGridView_Balance_CellValueChanged(object sender, DevExpress.XtraGrid.Views.Base.CellValueChangedEventArgs e)
        {
            try
            {
                if (e.Column.FieldName == "StockQty")
                {
                    GridView view = sender as GridView;
                    dtStQty = cls.GetData("sp_BACheckStockQty", new string[] { "@MADID", "@MALDIDs" }, new object[] { Convert.ToInt32(view.GetFocusedDataRow()["MADID"]), view.GetFocusedDataRow()["MALDIDs"].ToString() });
                    int NewQty = Convert.ToInt32(view.GetFocusedDataRow()["StockQty"]);
                    if ((Convert.ToInt32(dtStQty.Rows[0][1]) - NewQty) >= 0)
                        cls.UpdateData("sp_WHExctAllocateUpdate", new string[] { "@MADID", "@MALDIDs", "@Qty" }, new object[] { Convert.ToInt32(view.GetFocusedDataRow()["MADID"]), view.GetFocusedDataRow()["MALDIDs"].ToString(), NewQty });
                    LoadData();
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
    }
}
    